package com.bRoom.controller.back;

import com.bRoom.common.Conts;
import com.bRoom.common.ServerResponse;
import com.bRoom.pojo.Category;
import com.bRoom.pojo.User;
import com.bRoom.service.ICategoryService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("back/category")
public class ICategoryManageController {

    @Autowired
    private ICategoryService iCategoryService;

    @RequestMapping("add_category.do")
    @ResponseBody
    public ServerResponse<Category>  addCategory(Category category, HttpSession session){
        User user=(User) session.getAttribute(Conts.CURRENT_USER);
        if (user==null){
              return ServerResponse.createByErrorMsg("用户未登录，请先登录");
        }
         if (user.getUserRole()==Conts.ROLE.ROLE_SUPPER){
                   return iCategoryService.addCategory(category);
         }
         return ServerResponse.createByErrorMsg("对不起，你没有该权利");
    }


    @RequestMapping("update_category.do")
    @ResponseBody
    public  ServerResponse<Category> updateCategory(Category category,HttpSession session){

        User user=(User) session.getAttribute(Conts.CURRENT_USER);
        if (user==null){
            return ServerResponse.createByErrorMsg("用户未登录，请先登录");
        }
        if (user.getUserRole()==Conts.ROLE.ROLE_SUPPER){
             return iCategoryService.updateCategory(category);
        }
        return ServerResponse.createByErrorMsg("对不起，你没有该权利");
    }

    @RequestMapping("category_list.do")
    @ResponseBody
    public ServerResponse<PageInfo> categoryList(HttpSession session, @RequestParam(value = "pageSize",defaultValue = "1") int pageSize,@RequestParam(value = "pageNum",defaultValue = "3") int pageNum){
        User user=(User) session.getAttribute(Conts.CURRENT_USER);
        if (user==null){
            return ServerResponse.createByErrorMsg("用户未登录，请先登录");
        }
        if (user.getUserRole()==Conts.ROLE.ROLE_SUPPER||user.getUserRole()==Conts.ROLE.ROLE_ADMIN){
            return iCategoryService.categoryList(pageSize,pageNum);
        }
        return ServerResponse.createByErrorMsg("对不起，你没有该权利");
    }

    @RequestMapping("category_detail.do")
    @ResponseBody
    public  ServerResponse categoryDetail(Integer categoryId,HttpSession session){
        User user=(User) session.getAttribute(Conts.CURRENT_USER);
        if (user==null){
            return ServerResponse.createByErrorMsg("用户未登录，请先登录");
        }
        if (user.getUserRole()==Conts.ROLE.ROLE_SUPPER||user.getUserRole()==Conts.ROLE.ROLE_ADMIN){
            return iCategoryService.categoryDetail(categoryId);
        }
        return ServerResponse.createByErrorMsg("对不起，你没有该权利");
    }


    @RequestMapping("find_parentId.do")
    @ResponseBody
    public ServerResponse<List<Category>>  fingCategoryId(@RequestParam(value = "parentId",defaultValue = "0") Integer parentId,HttpSession session){
        User user=(User) session.getAttribute(Conts.CURRENT_USER);
        if (user==null){
            return ServerResponse.createByErrorMsg("用户未登录，请先登录");
        }
        if (user.getUserRole()==Conts.ROLE.ROLE_SUPPER||user.getUserRole()==Conts.ROLE.ROLE_ADMIN){
            return iCategoryService.fingCategoryId(parentId);
        }
        return ServerResponse.createByErrorMsg("对不起，你没有该权利");
    }

    @RequestMapping("find_deep_ChildrenCategory.do")
    @ResponseBody
    public ServerResponse finddeepChildrenCategory(Integer categoryId,HttpSession session){
        User user=(User) session.getAttribute(Conts.CURRENT_USER);
        if (user==null){
            return ServerResponse.createByErrorMsg("用户未登录，请先登录");
        }
        if (user.getUserRole()==Conts.ROLE.ROLE_SUPPER||user.getUserRole()==Conts.ROLE.ROLE_ADMIN){
            return iCategoryService.finddeepChildrenCategory(categoryId);
        }
        return ServerResponse.createByErrorMsg("对不起，你没有该权利");
    }
}
